package com.bjtech.web.action.management;

import java.math.BigDecimal;

import com.bjtech.dao.HeatMetaDataDao;
import com.bjtech.dao.HeatMeterInfoDao;
import com.bjtech.domain.HeatMetaData;
import com.bjtech.domain.query.HeatMetaDataQuerySpec;
import com.bjtech.web.action.LyheatBaseAction;

public class HeatMetaDataAction extends LyheatBaseAction {

	private static final long serialVersionUID = 1L;
	private HeatMetaDataDao heatMetaDataDao;
	private HeatMeterInfoDao heatMeterInfoDao;
	private HeatMetaDataQuerySpec selectBean;
	
	public void prepare() throws Exception {

	}
	
	public HeatMetaDataAction() throws Exception{
		model = new HeatMetaData();
		selectBean = new HeatMetaDataQuerySpec();
	}
	
	public void setHeatMetaDataDao(HeatMetaDataDao heatMetaDataDao) {
		this.heatMetaDataDao = heatMetaDataDao;
	}

	public String execute(){
		return this.list();
	}
	
	public String list(){
		selectBean.queryParamString();
		list = this.heatMetaDataDao.selectEntityPaginatedList(new HeatMetaData(),selectBean);
		return "list";
	}
	
	public String add(){
		return "edit";
	}
	
	public String save(){
		HeatMetaData entity = (HeatMetaData)model;
		
		if(entity.getSum_heat_unit()!=null){
			switch(entity.getSum_heat_unit().intValue()){
			case 1:entity.setSum_heat_mwh(new BigDecimal(entity.getSum_heat().doubleValue()/1000).setScale(2, BigDecimal.ROUND_HALF_DOWN));break;
			case 2:entity.setSum_heat_mwh(entity.getSum_heat());break;
			case 3:entity.setSum_heat_mwh(new BigDecimal(entity.getSum_heat().doubleValue()/0.0036).setScale(2, BigDecimal.ROUND_HALF_DOWN));break;
			}
		}
		
		if(entity.getId()==null){
			//热量表表号存在性验证
			Long result = heatMeterInfoDao.selectheatMetaInfoByMeterNum(entity.getMeter_num());
			if(result!=null&result.intValue()<=0){
				super.renderJavaScript("window.onload=function(){alert('热量表表号不存在，请您核对后重新输入！');location.href='HeatMetaData!add.action';}");
				return null;
			}
			heatMetaDataDao.insertEntity(entity);
		}else{
			heatMetaDataDao.updateEntity(entity);
		}
		return "success";
	}
	
	public String edit(){
		model = this.heatMetaDataDao.selectEntity(new HeatMetaData(),id);
		return "edit";
	}

	public String view(){
		model = this.heatMetaDataDao.selectEntity(new HeatMetaData(),id);
		return "view";
	}
	
	public String delete(){
		model = this.heatMetaDataDao.deleteEntity(new HeatMetaData(),id);
		return "success";
	}
	
	public HeatMetaDataQuerySpec getSelectBean() {
		return selectBean;
	}

	public void setSelectBean(HeatMetaDataQuerySpec selectBean) {
		this.selectBean = selectBean;
	}

	public HeatMeterInfoDao getHeatMeterInfoDao() {
		return heatMeterInfoDao;
	}

	public void setHeatMeterInfoDao(HeatMeterInfoDao heatMeterInfoDao) {
		this.heatMeterInfoDao = heatMeterInfoDao;
	}
}
